/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { EditorController } from '@ibiz-template/runtime';
import { IDatePicker } from '@ibiz/model-core';

export class CheckinTimePickerController extends EditorController<IDatePicker> {
  /**
   * 根据编辑器类型获取格式化
   *
   * @author lxm
   * @date 2022-11-03 16:11:21
   * @protected
   * @returns {*}  {string}
   */
  getFormatByType(editorType: string | undefined): string {
    switch (editorType) {
      // 时间选择器
      case 'DATEPICKER':
      case 'MOBDATE':
        return 'YYYY-MM-DD HH:mm:ss';
      // 时间选择控件
      case 'DATEPICKEREX':
        return 'YYYY-MM-DD HH:mm:ss';
      // 时间选择控件_无小时
      case 'DATEPICKEREX_NOTIME':
      case 'MOBDATE_NOTIME':
        return 'YYYY-MM-DD';
      // 时间选择控件_小时
      case 'DATEPICKEREX_HOUR':
      case 'MOBDATE_HOUR':
        return 'YYYY-MM-DD HH';
      // 时间选择控件_分钟
      case 'DATEPICKEREX_MINUTE':
      case 'MOBDATE_MINUTE':
        return 'YYYY-MM-DD HH:mm';
      // 时间选择控件_秒钟
      case 'DATEPICKEREX_SECOND':
      case 'MOBDATE_SECOND':
        return 'YYYY-MM-DD HH:mm:ss';
      // 时间选择控件_无日期
      case 'DATEPICKEREX_NODAY':
      case 'MOBDATE_NODAY':
        return 'HH:mm:ss';
      // 时间选择控件_无日期无秒钟
      case 'DATEPICKEREX_NODAY_NOSECOND':
      case 'MOBDATE_NODAY_NOSECOND':
        return 'HH:mm';
      // 时间选择控件_无秒钟
      case 'DATEPICKEREX_NOSECOND':
        return 'YYYY-MM-DD HH:mm';
      default:
        return 'YYYY-MM-DD HH:mm:ss';
    }
  }

  /**
   * 值格式化
   * @return {*}
   * @author: zhujiamin
   * @Date: 2022-08-25 14:33:14
   */
  get valueFormat(): string | undefined {
    if (super.valueFormat) {
      return super.valueFormat;
    }
    if (this.model.dateTimeFormat) {
      return this.model.dateTimeFormat;
    }
    return this.getFormatByType(this.model.editorType);
  }

  /**
   * 禁用的小时
   *
   * @type {number[]}
   * @memberof TimePickerController
   */
  public disabledHours: Array<number> = [];

  /**
   * 禁用的分钟
   *
   * @type {number[]}
   * @memberof TimePickerController
   */
  disabledMinutes: Array<number> = [];

  /**
   * 禁用时间范围
   *
   * @type {(IData | null)}
   * @memberof TimePickerController
   */
  disabledRange: IData[] | null = null;

  protected async onInit(): Promise<void> {
    super.onInit();
    if (this.editorParams?.disabledHours) {
      this.disabledHours = this.editorParams.disabledHours
        ?.split(',')
        .map((item: any) => Number(item));
    }
    if (this.editorParams?.disabledMinutes) {
      this.disabledMinutes = this.editorParams.disabledMinutes
        ?.split(',')
        .map((item: any) => Number(item));
    }
  }

  /**
   * 设置禁用小时
   *
   * @param {number[]} disabledHours
   * @memberof TimePickerController
   */
  setDisabledHours(disabledHours: number[]) {
    this.disabledHours = disabledHours;
  }

  /**
   * 设置禁用分钟
   *
   * @param {number[]} disabledMinutes
   * @memberof TimePickerController
   */
  setDisabledMinutes(disabledMinutes: number[]) {
    this.disabledMinutes = disabledMinutes;
  }

  /**
   * 禁用时间范围
   *
   * @param {IData} dateRange
   * @memberof TimePickerController
   */
  setDisabledRange(dateRange: IData[]) {
    this.disabledRange = dateRange;
    const hours: number[] = [];
    // 计算禁用小时
    this.disabledRange.forEach(item => {
      const { starthour, startminute, endhour, endminute } = item;
      if (starthour < endhour) {
        for (let i = starthour + 1; i < endhour; i++) {
          hours.push(i);
        }
        if (startminute === 0) {
          hours.push(starthour);
        }
        if (endminute === 59) {
          hours.push(endhour);
        }
      }
    });
    this.setDisabledHours(hours);
  }

  /**
   * 处理禁用范围
   *
   * @param {string} date
   * @return {*}
   * @memberof TimePickerController
   */
  handleDisabledRange(date: string) {
    if (!date || !this.disabledRange) {
      return;
    }
    const [hour, minute] = date.split(':').map(item => Number(item));
    this.disabledMinutes = [];
    this.disabledRange.forEach(item => {
      const { starthour, startminute, endhour, endminute } = item;
      const minutes = [];

      if (hour === starthour && hour === endhour) {
        for (let i = 0; i <= 59; i++) {
          if (i >= startminute && i <= endminute) {
            minutes.push(i);
          }
        }
        this.setDisabledMinutes(minutes);
      }
      if (hour === starthour && endhour > hour) {
        for (let i = 0; i <= 59; i++) {
          if (i >= startminute) {
            minutes.push(i);
          }
        }
        this.setDisabledMinutes(minutes);
      }
      if (endhour === hour && endhour > starthour) {
        for (let i = 0; i <= endminute; i++) {
          if (i <= endminute) {
            minutes.push(i);
          }
        }
        this.setDisabledMinutes(minutes);
      }
      if (starthour < hour && endhour > hour) {
        for (let i = 0; i <= 59; i++) {
          minutes.push(i);
        }
        this.setDisabledMinutes(minutes);
      }
    });
  }
}
